/**
 * Copyright (C) 2011 Andrew C. Love (DNC) <dnc.app.sup@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.dnc.cloak.framework.model;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;


public class AbstractModel implements Model, Serializable {
	
	private String lstUptUser;
	private Date lstUptTime;
	private boolean isDelete = false;
	private boolean isUpdate = false;
    private boolean isInsert = false;
    private boolean isSelect = false;
    private Map<String,Object> properties = null;
    private final PropertyChangeSupport pcs = new PropertyChangeSupport( this );
    private String ID = this.getClass().getName();
    
	public String getID() {
		return this.ID;
	}
	
	public void setID(String ID) {
		this.ID = ID;
	}

    public PropertyChangeSupport getPcs() {
		return pcs;
	}

	public String getLstUptUser() {
		return lstUptUser;
	}

	public void setLstUptUser(String lstUptUser) {
		pcs.firePropertyChange("lstUptUser", this.lstUptUser, this.lstUptUser=lstUptUser);
	}
	
	public Date getLstUptTime() {
		return lstUptTime;
	}
	
	public void setLstUptTime(Date lstUptTime) {
		pcs.firePropertyChange("lstUptTime", this.lstUptTime, this.lstUptTime=lstUptTime);
	}
	
	public boolean isDelete() {		
		return isDelete;
	}
	
	public void setDelete(boolean delete) {
		pcs.firePropertyChange("delete", this.isDelete, this.isDelete=delete);		
	}
	
	public boolean isInsert() {
		return this.isInsert;
	}	
	
	public void setInsert(boolean isInsert) {
		pcs.firePropertyChange("insert", this.isInsert, this.isInsert=isInsert);		
	}
	
	public boolean isUpdate() {
		return isUpdate;
	}
	
	public void setUpdate(boolean isUpdate) {
		pcs.firePropertyChange("update", this.isUpdate, this.isUpdate=isUpdate);		
	}
    
	public boolean isSelect() {
        return isSelect;
    }
    
	public void setSelect(boolean isSelect) {
		pcs.firePropertyChange("select", this.isSelect, this.isSelect=isSelect);		
    }    
    
	public void setProperty(String key, Object value) {		
        if (this.properties == null) {
            this.properties = new TreeMap<String,Object>();
        }
        
		Object old = this.properties.get(key);
		this.properties.put(key,value);
		this.pcs.firePropertyChange( "properties."+key, old, value);
    }
    
	public Object getProperty(String key) {		
        if (this.properties == null) {
            return null;
        }
        
        return this.properties.get(key);        
    }
    
	public boolean isEditable(String columnName,Map<String,Object> properties) {
        return true;
    }    
    
    public void resetCrud() {    	
        this.setDelete(false);
        this.setSelect(false);
        this.setInsert(false);
        this.setUpdate(false);        
    }
    
    public void addPropertyChangeListener(PropertyChangeListener listener) {
		this.pcs.addPropertyChangeListener(listener);
	}

	public void removePropertyChangeListener(PropertyChangeListener listener) {
		this.pcs.removePropertyChangeListener(listener);
	}    

}
